﻿using System;
using System.Collections.Generic;
using Yaml.Presentation.Tokens;

namespace Yaml.Presentation.Parsing {
    public class TokenQueue {
        private List<Token> tokens;
        private int tokensTaken;

        /// <summary>
        /// Create a new TokenQueue
        /// </summary>
        public TokenQueue() {
            tokens = new List<Token>();
            tokensTaken = 0;
        }

        /// <summary>
        /// Push a token into the back of the queue
        /// </summary>
        /// <param name="t"></param>
        public void Push(Token t) {
            tokens.Add(t);
        }

        /// <summary>
        /// Take a peek at the token at the front of the queue without removing it
        /// </summary>
        /// <returns>The token at the front of the queue or null if the queue is empty</returns>
        public Token Peek() {
            if (tokens.Count > 0) {
                return tokens[0];
            }
            return null;
        }

        /// <summary>
        /// Pop a token from the queue
        /// </summary>
        /// <returns>The token at the front of the queue or null if the queue is empty</returns>
        public Token Pop() {
            if (tokens.Count > 0) {
                Token t = tokens[0];
                tokens.RemoveAt(0);
                tokensTaken++;
                return t;
            }
            return null;
        }

        /// <summary>
        /// Insert the specified token in the queue at the specified position in the queue.
        /// </summary>
        /// <param name="index">The index at which the token is inserted.  0 is the front of the queue, Count is the back.</param>
        public void Insert(int index, Token t) {
            tokens.Insert(index, t);
        }

        /// <summary>
        /// Clear the queue
        /// </summary>
        public void Clear() {
            tokens.Clear();
        }

        /// <summary>
        /// The current length of the queue
        /// </summary>
        public int Count {
            get { return tokens.Count; }
        }

        /// <summary>
        /// The number of tokens that have been popped from the queue
        /// </summary>
        public int TokensTaken {
            get { return tokensTaken; }
        }

        public int NextTokenNumber {
            get { return tokensTaken + tokens.Count; }
        }
    }
}
